//
//  DSYButtonFTableViewCell.m
//  BLDMusics
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "DSYButtonFTableViewCell.h"
#import "DSYBLButton.h"
#import "DSYLocalViewController.h"

@interface DSYButtonFTableViewCell ()

@property (nonatomic, retain) DSYBLButton *localButton;
@property (nonatomic, retain) DSYBLButton *songDownButton;
@property (nonatomic, retain) DSYBLButton *mvDownButton;
@property (nonatomic, retain) DSYBLButton *latestPlayButton;

@end

@implementation DSYButtonFTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatSubviews];
    }
    return self;
}



- (void)creatSubviews {
    
    self.localButton = [[DSYBLButton alloc] initWithFrame:CGRectMake(10, 10, (SCREEN_WIDTH - 30) / 2, (self.contentView.frame.size.height - 30) / 2)];
    _localButton.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:_localButton];
    [_localButton addTarget:self action:@selector(localVC:) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat localButton_W = CGRectGetWidth(_localButton.frame);
    CGFloat localButton_H = CGRectGetHeight(_localButton.frame);
    
    self.songDownButton = [[DSYBLButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_localButton.frame) + 10, CGRectGetMinY(_localButton.frame), localButton_W, localButton_H)];
    [self.contentView addSubview:_songDownButton];
    _songDownButton.backgroundColor = [UIColor cyanColor];
    [_songDownButton addTarget:self action:@selector(songDownAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.mvDownButton = [[DSYBLButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(_localButton.frame), CGRectGetMaxY(_localButton.frame) + 10, localButton_W, localButton_H)];
    [self.contentView addSubview:_mvDownButton];
    _mvDownButton.backgroundColor = [UIColor cyanColor];
    
    self.latestPlayButton = [[DSYBLButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(_songDownButton.frame), CGRectGetMinY(_mvDownButton.frame), localButton_W, localButton_H)];
    [self.contentView addSubview:_latestPlayButton];
    _latestPlayButton.backgroundColor = [UIColor cyanColor];
    [_latestPlayButton addTarget:self action:@selector(lyricAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)layoutSubviews
{
    self.localButton.frame = CGRectMake(10, 10, (SCREEN_WIDTH - 30) / 2, (self.contentView.frame.size.height - 30) / 2);
    CGFloat localButton_W = CGRectGetWidth(_localButton.frame);
    CGFloat localButton_H = CGRectGetHeight(_localButton.frame);
    
    self.songDownButton.frame = CGRectMake(CGRectGetMaxX(_localButton.frame) + 10, CGRectGetMinY(_localButton.frame), localButton_W, localButton_H);
    self.mvDownButton.frame = CGRectMake(CGRectGetMinX(_localButton.frame), CGRectGetMaxY(_localButton.frame) + 10, localButton_W, localButton_H);
    self.latestPlayButton.frame = CGRectMake(CGRectGetMinX(_songDownButton.frame), CGRectGetMinY(_mvDownButton.frame), localButton_W, localButton_H);
    
    
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *plistPath = [documentPath stringByAppendingPathComponent:@"songList.plist"];
    
    NSArray *plistArr = [NSArray arrayWithContentsOfFile:plistPath];
    
    NSString *str = [NSString stringWithFormat:@"%ld首歌曲", plistArr.count];
    [self.localButton installImage:@"iconfont-ttpodicon" Title:@"本地音乐" Detail:str];
    [self.songDownButton installImage:@"iconfont-xiazaigequ" Title:@"歌曲下载" Detail:@"没有下载任务"];
    [self.mvDownButton installImage:@"iconfont-mvxiazai" Title:@"MV下载" Detail:@"没有下载任务"];
    [self.latestPlayButton installImage:@"iconfont-zuijinbofang" Title:@"最近播放" Detail:@"没有播放"];
    
    
    
}



- (void)localVC:(UIButton *)button {
    DSYLocalViewController *localVc = [[DSYLocalViewController alloc] init];
    
    localVc.url = @"";
    localVc.buttonIndex = 0;
    
    UINavigationController *nav = [self getViewController];
    [nav pushViewController:localVc animated:YES];
    
}

- (void)lyricAction:(UIButton *)button {
    DSYLocalViewController *localVc = [[DSYLocalViewController alloc] init];
    
    localVc.url = @"http://project.lanou3g.com/teacher/UIAPI/MusicInfoList.plist";
    localVc.buttonIndex = 3;
    
    UINavigationController *nav = [self getViewController];
    [nav pushViewController:localVc animated:YES];
}


- (void)songDownAction:(UIButton *)button {
    
    DSYLocalViewController *localVc = [[DSYLocalViewController alloc] init];
    
    localVc.url = @"http://api.songlist.ttpod.com/songlists/305940146?app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.2.1&active=1&net=2&openudid=921fb117b843753583dbf257222bb96a79d3c64e&idfa=5B1500CB-A041-4C95-951A-F6408B90A0BF&utdid=Vule3TyuSLwDAKd%2FfPxZrXTy&alf=201200&bundle_id=com.ttpod.music&latitude=38.88269196885403&longtitude=121.539207307381";
    localVc.buttonIndex = 1;
    
    UINavigationController *nav = [self getViewController];
    [nav pushViewController:localVc animated:YES];
    
}

// 获取当前navigation
- (UINavigationController *)getViewController {
    
    for (UIView* next = [self superview];next; next = next.superview) {
        
        UIResponder* nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UINavigationController class]]) {
            
            return (UINavigationController *)nextResponder;
            
        }
        
    }
    return nil;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
